package deviceservice.core;

import com.alibaba.fastjson.JSON;
import deviceservice.entity.reponse.DevAddResp;
import deviceservice.entity.req.DevAddReq;
import deviceservice.token.AuthToken;
import deviceservice.utils.CryptoUtil;
import deviceservice.utils.DataMap;

/**
 * Created by Jason on 2017/8/22.
 */
public class Device {
    public static DataMap getaddDeviceres (String username, String key, String url, DevAddReq devAddReq){

        String token = AuthToken.getAddreqToken(devAddReq.getTimestamp(),username,key,url);

        DevAddResp devAddResp = new DevAddResp();
        String accessToken=null;
        long timestamp = System.currentTimeMillis();
        if(token.contentEquals(devAddReq.getToken())){
            accessToken = AuthToken.getAccessToken(devAddReq.getTimestamp(),timestamp,username,key);
            devAddResp.setResult(100);
            devAddResp.setMessage("Verification Successful");
        }
        else{
            devAddResp.setResult(200);
            devAddResp.setMessage("failed");}


        devAddResp.setTimestamp(timestamp);
        String responestoken = AuthToken.getAddresToken(devAddResp.getResult(), devAddReq.getTimestamp(),timestamp,username,key);
        devAddResp.setToken(responestoken);
        DataMap dataMap = new DataMap();
        dataMap.put("res",JSON.toJSONString(devAddResp));
        if(accessToken!=null || !accessToken.isEmpty())
            dataMap.put("accesstoken",accessToken);
        return dataMap;
    }
    public static DataMap getDeviceIDres(String accessToken,String username, DevAddReq devAddReq){

        String token = AuthToken.getDevReqToken(devAddReq.getTimestamp(),accessToken, devAddReq.getDevID(), devAddReq.getHeart(), devAddReq.getLogIP());

        DevAddResp devAddResp = new DevAddResp();
        DataMap result = new DataMap();
        devAddResp.setResult(900);
        if(token.contentEquals(devAddReq.getToken()))
        {
            devAddResp.setResult(100);
            devAddResp.setMessage("Verification Successful");
            result.put("result",100);
                 result.put("deviceID", devAddReq.getDevID());
                 result.put("logIP", devAddReq.getLogIP());
                 result.put("heart", devAddReq.getHeart());
                 result.put("statusQu",devAddReq.getLogTypeMap().get(11));
                 result.put("maskingQu",devAddReq.getLogTypeMap().get(12));
                 result.put("oprationQu",devAddReq.getLogTypeMap().get(13));
                 if(devAddReq.getLogUser()!=null || devAddReq.getLogPass()!=null){
                     result.put("logUser", devAddReq.getLogUser());
                     result.put("logPass",CryptoUtil.aesDecrypt(devAddReq.getLogPass(),CryptoUtil.hash(accessToken)));
                 }

        }
        else{
            devAddResp.setResult(200);
            devAddResp.setMessage("Verification failed");
            result.put("result",200);
        }
        result.put("responseString",JSON.toJSON(devAddResp));
        return result;
    }
    public boolean getloginAuthResult(String accessToken,String devID, DevAddReq devAddReq){
        String token = AuthToken.getLoginToken(accessToken,devAddReq.getTimestamp(),devID);
        if(token.contentEquals(devAddReq.getToken())){
           return  true;
        }
        else
            return false;

    }
    public DataMap getFill(String accessToken,String devID, DevAddReq devAddReq){
        String token = AuthToken.getFillToken(accessToken,devAddReq.getTimestamp(),devAddReq.getCommand(),devID);
        DataMap dataMap = new DataMap();
        dataMap.put("ts",devAddReq.getTimestamp()+1);
        if(token.contentEquals(devAddReq.getToken())){
            dataMap.put("result",100);
            dataMap.put("command",devAddReq.getCommand());

        }
        else
            dataMap.put("result",200);
        return dataMap;
    }


}
